package gui;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagLayout;

import javax.crypto.SecretKey;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;

import java.awt.Dimension;
import javax.swing.JLabel;
import java.awt.Rectangle;
import java.io.File;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.cert.Certificate;
import java.util.HashMap;

import javax.swing.JButton;

import code.KeyStoreManager;
import code.Language;
import code.Settings;
import code.SymmetricEncDec;

public class SignDigitalSignature extends JPanel {

	Color c= new Color(0x80, 0x80, 0xFF);
	
	private static final long serialVersionUID = 1L;
	
	ButtonGroup rbgroup2 = new ButtonGroup();
	private JPanel jContentPane = null;
	private JTabbedPane jTabbedPane = null;
	private JPanel jPanel = null;
	private JButton jButton = null;
	private JButton jButton2 = null;
	private JRadioButton jRadioButton3 = null;
	private JRadioButton jRadioButton4 = null;
	private JLabel jLabel3 = null;
	private JLabel jLabel4 = null;
	private JLabel jLabel5 = null;
	private JLabel jLabel6 = null;
	private JLabel jLabel7 = null;
	private JLabel jLabel8 = null;
	private JLabel jLabel9 = null;
	private JLabel jLabel10 = null;
	private JLabel jLabel11 = null;
	private JLabel jLabel12 = null;
	private JTextArea jTextArea = null;
	
	Color TAc = new Color(246, 250, 250);
	String nextclass;
	private PublicKey publicKey;
	private PrivateKey privateKey;
	private Certificate cert;
	private String file;

	private HashMap labels;
	
	ATOCOmain atocoframe;
	private KeyStoreManager ksm;
	private Settings s;
	
	/**
	 * This is the default constructor
	 */
	public SignDigitalSignature(ATOCOmain atocoframe) {
		super();
		s = new Settings();
		Language l = new Language();
		labels = l.changeLanguage((String) s.global.get("language"), "SignDigitalSignature", (String) s.global.get("installPath"));
		this.atocoframe=atocoframe;
		initialize();
		this.setPreferredSize( new Dimension( 400, 600 ) );
		
		// Every Keystore operation must use this one.
		ksm = new KeyStoreManager(
				(String)Settings.global.get("personalKeyStoreFile"), 
				(String)Settings.global.get("personalKeyStorePassword"));
		if (ksm.getKeyStore() == null) {
        	System.out.println("Keystore file not found. Creating one.");
        	ksm.createKeyStore(
        			(String)Settings.global.get("personalKeyStoreFile"), 
    				(String)Settings.global.get("personalKeyStorePassword"));
        }
		
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(852, 451);
		this.setLayout(null);
		this.add(getJTabbedPane(), null);
		this.setBackground(Color.WHITE);
	}
	
	private JTabbedPane getJTabbedPane() {
		if (jTabbedPane == null) {
			jTabbedPane = new JTabbedPane();
			jTabbedPane.setBounds(new Rectangle(25, 70, 800, 450));
			jTabbedPane.addTab((String) labels.get("tab"), null, getJPanel(), null);
		}
		return jTabbedPane;
	}
	
	private JPanel getJPanel() {
		if (jPanel == null) {
			jLabel12   = new JLabel(new ImageIcon((String) s.global.get("installPath") + File.separator + "images/en/accepticon.gif"));
			jLabel12.setBounds(new Rectangle(5, 250, 25, 25));
			jLabel12.setVisible(false);
			jLabel11   = new JLabel(new ImageIcon((String) s.global.get("installPath") + File.separator + "images/en/accepticon.gif"));
			jLabel11.setBounds(new Rectangle(5, 155, 25, 25));
			jLabel11.setVisible(false);
			jLabel10   = new JLabel(new ImageIcon((String) s.global.get("installPath") + File.separator + "images/en/accepticon.gif"));
			jLabel10.setBounds(new Rectangle(5, 30, 25, 25));
			jLabel10.setVisible(false);
			jLabel9   = new JLabel((String) labels.get("StatusMessage"));
			jLabel9.setBounds(new Rectangle(30, 270, 70, 25));
			jLabel8   = new JLabel((String) labels.get("Key"));
			jLabel8.setBounds(new Rectangle(30, 155, 120, 25));
			jLabel7  = new JLabel((String) labels.get("File"));
			jLabel7.setBounds(new Rectangle(30, 30, 120, 25));
			jLabel5 = new JLabel(new ImageIcon((String) s.global.get("installPath") + File.separator + "images/en/questionmark.jpg"));
			jLabel5.setBounds(new Rectangle(40, 210, 25, 25));
			jLabel5.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					jTextArea.setText((String) labels.get("oldKey"));
				}
			});
			jLabel4 = new JLabel(new ImageIcon((String) s.global.get("installPath") + File.separator + "images/en/questionmark.jpg"));
			jLabel4.setBounds(new Rectangle(40, 185, 25, 25));
			jLabel4.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					jTextArea.setText((String) labels.get("newKey"));
				}
			});
			jLabel6 = new JLabel(new ImageIcon((String) s.global.get("installPath") + File.separator + "images/en/nextmiddle.gif"));
			jLabel6.setBounds(new Rectangle(250, 180, 50, 50));
			jLabel6.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					if(nextclass != null){
						if(nextclass.equalsIgnoreCase("newkeygui")){
							atocoframe.contentPane(new KeyCreationAsymmetric(atocoframe, passclass(), "Sign"));
						}else if(nextclass.equalsIgnoreCase("selectkeygui")){
							atocoframe.contentPane(new AsymmetricKeyList(atocoframe, passclass(), "Sign", "dec"));
						}
					}

				}
			});
			jLabel3  = new JLabel();
			jLabel3.setBounds(new Rectangle(30, 110, 350, 20));
			jLabel3.setText((String) labels.get("Path"));
			jLabel3.setFont(new Font("Arial", Font.ITALIC, 12));
			//jLabel3.setBorder(BorderFactory.createMatteBorder(0, 0, 5, 0, c));
			jPanel = new JPanel();
			jPanel.setLayout(null);
			jPanel.setBackground(Color.WHITE);
			jPanel.add(getJButton(), null);
			jPanel.add(getJButton2(), null);
			jPanel.add(getJTextArea(), null);
			jPanel.add(getJRadioButton3(), null);
			jPanel.add(getJRadioButton4(), null);
			jPanel.add(jLabel3, null);
			jPanel.add(jLabel4, null);
			jPanel.add(jLabel5, null);
			jPanel.add(jLabel6, null);
			jPanel.add(jLabel7, null);
			jPanel.add(jLabel8, null);
			jPanel.add(jLabel9, null);
			jPanel.add(jLabel10, null);
			jPanel.add(jLabel11, null);
			jPanel.add(jLabel12, null);
		}
		return jPanel;
	}
	
	private JTextArea getJTextArea() {
		if (jTextArea == null) {
			jTextArea = new JTextArea();
			jTextArea.setBounds(new Rectangle(340, 42, 273, 277));
			jTextArea.setBackground(TAc);
			jTextArea.setLineWrap(true);
		}
		return jTextArea;
	}
	
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton();
			jButton.setBounds(new Rectangle(40, 300, 100, 40));
			jButton.setText((String) labels.get("Sign"));
			jButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
//					if(!publicKey.equals(null)||!privateKey.equals(null)||!file.equals(null)) {
					if(!cert.equals(null)||!privateKey.equals(null)||!file.equals(null)) {
//						ksm.signFile(privateKey, publicKey, file, file + ".signed", file + ".crt");
						ksm.signFile(privateKey, cert, file, file + ".signature", file + ".crt");
						jLabel12.setVisible(true);
					}
				}
			});
		}
		return jButton;
	}
	
	public void browse(){
		JFileChooser fc = new JFileChooser(); 
		fc.setDialogTitle((String) labels.get("fc"));
		fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
		fc.setMultiSelectionEnabled(true);
		fc.showOpenDialog(this);

		try { 
			// Open an input stream 
			File f= fc.getSelectedFile();
			//File[] f =fc.getSelectedFiles();
//			System.out.println(f);
			if(!f.equals("null")){
				String s=f.getPath();
				setFile(s);

			}

		}catch(Exception e){

		}

	}
	private JButton getJButton2() {
		if (jButton2 == null) {
			jButton2 = new JButton(new ImageIcon((String) s.global.get("installPath") + File.separator + "images/en/klasor.gif"));
			jButton2.setText((String) labels.get("ChooseFile"));
			jButton2.setBounds(new Rectangle(75, 60, 155, 40));
			jButton2.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println("actionPerformed()"); 
					browse();
				}
			});
		}
		return jButton2;
	}
	
	private JRadioButton getJRadioButton3() {
		if (jRadioButton3 == null) {
			jRadioButton3 = new JRadioButton();
			jRadioButton3.setBounds(new Rectangle(75, 185, 158, 21));
			jRadioButton3.setText((String) labels.get("CreateNewKey"));
			jRadioButton3.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e){
					if(jRadioButton3.isSelected())
					{
						nextclass="newkeygui";
					}

				}
			});
			rbgroup2.add(jRadioButton3);
		}
		return jRadioButton3;
	}
	
	private JRadioButton getJRadioButton4() {
		if (jRadioButton4 == null) {
			jRadioButton4 = new JRadioButton();
			jRadioButton4.setBounds(new Rectangle(75, 210, 165, 21));
			jRadioButton4.setText((String) labels.get("UseExistingKey"));

			jRadioButton4.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e){
					if(jRadioButton4.isSelected())
					{
						nextclass="selectkeygui";
					}

				}
			});
			rbgroup2.add(jRadioButton4);
		}
		return jRadioButton4;
	}

	public void setKeys(PublicKey publicKey, PrivateKey privateKey){
		this.publicKey=publicKey;
		this.privateKey=privateKey;
		if(!publicKey.equals("null") || !privateKey.equals("null")){
			jLabel11.setVisible(true);
		}

	}
	
	public void setKeyAndCert(Certificate cert, PrivateKey privateKey){
		this.cert=cert;
		this.privateKey=privateKey;
		if(!cert.equals("null") || !privateKey.equals("null")){
			jLabel11.setVisible(true);
		}
	}

	public void setFile(String file){
		this.file=file;
		if(!file.equals("null")){
			jLabel3.setText((String) labels.get("Path")+": "+file);
			jLabel10.setVisible(true);
		}
	}
	
	public SignDigitalSignature passclass(){
		return this;
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"
